﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CriptoFile
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            isDirectory.Enabled = false;
        }

        private void cripta_Click(object sender, EventArgs e)
        {
            Crypto c = new Crypto();
            String key = stringKey.Text;
            String input = stringIn.Text;
            String output = stringOut.Text;
            bool execute = true;
            if (key == "")
            {
                MessageBox.Show("Errore chiave non valida", "Errore", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if (input == "")
                {
                    MessageBox.Show("Errore file da cpritare non valido", "Errore", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    if (output == "")
                    {
                        MessageBox.Show("Errore file di output non valido", "Errore", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        if (input == output)
                        {
                            if (MessageBox.Show("Attenzione se si preme ok il file originale verrà sovrascritto\nProcedere?",
                                "Attenzione", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel)
                                execute = false;
                        }
                            if (execute)
                            {
                                Cursor = Cursors.WaitCursor;
                                Crypto cr = new Crypto();
                                cr.CreateKey(key, key128.Checked);
                                if (isDirectory.Checked)
                                {
                                    //EncriptDirectory(stringIn.Text, cr);
                                    String[] files = Directory.GetFiles(stringIn.Text);
                                    foreach (String file in files)
                                    {
                                        Byte[] b = File.ReadAllBytes(file);
                                        b = cr.EncodeByte(b);

                                        File.WriteAllBytes(stringOut.Text + "\\" + Path.GetFileName(file), b);
                                        if (notReplace.Checked)
                                            File.Delete(file);
                                    }
                                    String[] directorys = Directory.GetDirectories(stringIn.Text);
                                    foreach (String directory in directorys)
                                    {
                                        EncriptDirectory(directory, cr);
                                    }
                                }
                                else
                                {
                                    Byte[] b = File.ReadAllBytes(input);
                                    b = cr.EncodeByte(b);
                                    File.WriteAllBytes(output, b);
                                    if (notReplace.Checked)
                                        File.Delete(input);
                                }
                                Cursor = Cursors.Default;
                                MessageBox.Show("Criptazione avvenuta corrttamente");
                            }
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Multiselect = false;
            openFileDialog1.Filter = "Tutti i file (*.*)|*.*";
            openFileDialog1.Title = "Seleziona il file da aprire";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                stringIn.Text = openFileDialog1.FileName;
            }
        }

        private void decripta_Click(object sender, EventArgs e)
        {
            Crypto c = new Crypto();
            String key = stringKey.Text;
            String input = stringIn.Text;
            String output = stringOut.Text;
            bool execute = true;
            if (key == "")
            {
                MessageBox.Show("Errore chiave non valida", "Errore", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if (input == "")
                {
                    MessageBox.Show("Errore file da cpritare non valido", "Errore", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    if (output == "")
                    {
                        MessageBox.Show("Errore file di output non valido", "Errore", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        if (input == output)
                        {
                            if (MessageBox.Show("Attenzione se si preme ok il file originale verrà sovrascritto\nProcedere?",
                                "Attenzione", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel)
                                execute = false;
                        }
                            if (execute)
                            {
                                Cursor = Cursors.WaitCursor;
                                Crypto cr = new Crypto();
                                cr.CreateKey(key, key128.Checked);
                                if (isDirectory.Checked)
                                {
                                    //DecriptDirectory(stringIn.Text, cr);
                                    String[] files = Directory.GetFiles(stringIn.Text);
                                    foreach (String file in files)
                                    {
                                        Byte[] b = File.ReadAllBytes(file);
                                        b = cr.DecodeByte(b);

                                        File.WriteAllBytes(stringOut.Text + "\\" + Path.GetFileName(file), b);
                                        if (notReplace.Checked)
                                            File.Delete(file);
                                    }
                                    String[] directorys = Directory.GetDirectories(stringIn.Text);
                                    foreach (String directory in directorys)
                                    {
                                        DecriptDirectory(directory, cr);
                                    }
                                }
                                else
                                {
                                    Byte[] b = File.ReadAllBytes(input);
                                    b = cr.DecodeByte(b);
                                    File.WriteAllBytes(output, b);
                                    if (notReplace.Checked)
                                        File.Delete(input);
                                }
                                Cursor = Cursors.Default;
                                MessageBox.Show("Decriptazione avvenuta corrttamente");
                            }
                        }

                    
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {


            saveFileDialog1.Title = "Seleziona il file da salvare";

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                stringOut.Text = saveFileDialog1.FileName;
            }
        }

        private void openFolder_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                stringIn.Text = folderBrowserDialog1.SelectedPath;
                stringOut.Text = folderBrowserDialog1.SelectedPath;
                isDirectory.Checked = true;
            }
        }

        private void saveDirecory_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                stringOut.Text = folderBrowserDialog1.SelectedPath;
                isDirectory.Checked = true;
            }
        }

        private void EncriptDirectory(String path, Crypto cr)
        {
            String[] files = Directory.GetFiles(path);
            foreach (String file in files)
            {
                Byte[] b = File.ReadAllBytes(file);
                b = cr.EncodeByte(b);
                String lastDir = path.Substring(path.LastIndexOf('\\') + 1);
                lastDir = stringOut.Text + "\\" + lastDir;
                if (!Directory.Exists(lastDir))
                    Directory.CreateDirectory(lastDir);
                File.WriteAllBytes(lastDir + "\\" + Path.GetFileName(file), b);
                if (notReplace.Checked)
                    File.Delete(file);
            }
            String[] directorys = Directory.GetDirectories(path);
            foreach (String directory in directorys)
            {
                EncriptDirectory(directory, cr);
            }
        }


        private void DecriptDirectory(String path, Crypto cr)
        {
            String[] files = Directory.GetFiles(path);
            foreach (String file in files)
            {
                Byte[] b = File.ReadAllBytes(file);
                b = cr.DecodeByte(b);
                String lastDir = path.Substring(path.LastIndexOf('\\') + 1);
                lastDir = stringOut.Text + "\\" + lastDir;
                if (!Directory.Exists(lastDir))
                    Directory.CreateDirectory(lastDir);
                File.WriteAllBytes(lastDir + "\\" + Path.GetFileName(file), b);
                if (notReplace.Checked)
                    File.Delete(file);
            }
            String[] directorys = Directory.GetDirectories(path);
            foreach (String directory in directorys)
            {
                DecriptDirectory(directory, cr);
            }
        }

        private void tutorialToolStripMenuItem_Click(object sender, EventArgs e)
        {
           // Tutorial t = new Tutorial();
           // t.ShowDialog();
            System.Diagnostics.Process.Start(Directory.GetCurrentDirectory() + "\\Tutorial\\Manuale.pdf");
        }

        private void stringIn_TextChanged(object sender, EventArgs e)
        {
            if (stringIn.Text == stringOut.Text)
            {
                replace.Checked = true;
                groupBox2.Enabled = false;
            }
            else
            {
                groupBox2.Enabled = true;
            }
        }

        private void stringOut_TextChanged(object sender, EventArgs e)
        {
            if (stringIn.Text == stringOut.Text)
            {
                replace.Checked = true;
                groupBox2.Enabled = false;
            }
            else
            {
                groupBox2.Enabled = true;
            }
        }

    }
}
